﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace PPI.Data
{
    public class DSettings
    {
        public static DataSet SelectKml(string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Vendor_Settings_SelectKML", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Vendor_Settings_SelectKML", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static void Update(int id, string value, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Vendor_Settings_Update", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_SettingId", ParameterDirection.Input, false, id));
                cmd.Parameters.Add(DBHelper.CreateIntParameter("@VendorFK", ParameterDirection.Input, false, DBNull.Value));
                cmd.Parameters.Add(DBHelper.CreateTextParameter("@Value", ParameterDirection.Input, false, value));

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }

        public static DataSet SelectSettingByKey(string key, string connectionKey)
        {
            SqlDataAdapter da = null;
            try
            {
                da = new SqlDataAdapter("up_Settings_SelectAllByKey", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.Parameters.Add(DBHelper.CreateNVarCharParameter("@key", 255, ParameterDirection.Input, false, key));
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception("Error executing up_Settings_SelectAllByKey", ex);
            }
            finally
            {
                if (da != null) da.SelectCommand.Connection.Close();
            }
        }

        public static void UpdateSettingByKey(string key, string text, string value, string connectionKey)
        {

            SqlCommand cmd = null;
            SqlTransaction tr = null;
            try
            {
                cmd = new SqlCommand("up_Settings_UpdateByKey", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(DBHelper.CreateNVarCharParameter("@key", 255, ParameterDirection.Input, false, key));
                cmd.Parameters.Add(DBHelper.CreateTextParameter("@text", ParameterDirection.Input, false, text));
                cmd.Parameters.Add(DBHelper.CreateTextParameter("@value", ParameterDirection.Input, false, value));

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }

        }
    }
}
